package com.unittec.zk.provider.controller;

import com.alibaba.fastjson.JSONObject;
import com.unittec.zk.provider.ApplicationContextProvider;
import com.unittec.zk.provider.log.Log;
import com.unittec.zk.provider.utils.FileUtils;
import com.unittec.zk.provider.utils.OrderNumUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;

/**
 * @author Jason
 * @date 2021/9/17
 */
@RestController
@Api(tags = "服务器文件上传下载管理")
public class UploadServerController {

    @Resource
    private Log log;

    @RequestMapping(value = "/uploadServerFile", method = RequestMethod.POST)
    @ApiOperation(value = "服务器文件上传", notes = "服务器文件上传")
    @ResponseBody
    public JSONObject uploadServerFile(@RequestPart(value = "file") MultipartFile file) {
        JSONObject jsonObject = new JSONObject();
        log.sysInfoLog("uploadServerFile", "服务器文件上传   begin");
        try {
            // 源文件名
            String originalFilename = file.getOriginalFilename();
            if (StringUtils.isNotBlank(originalFilename)) {
                if (FileUtils.checkSpecialStr(originalFilename)) {
                    jsonObject.put("code", 200);
                    jsonObject.put("msg", "请求成功");
                    jsonObject.put("biz_code", "500");
                    jsonObject.put("biz_msg", "上传文件名不能包含特殊字符");
                    return jsonObject;
                }
            }

            // 获取文件后缀名
            String suffixName = "";
            if (originalFilename.contains(".")) {
                suffixName = originalFilename.substring(originalFilename.lastIndexOf("."));
            }
            // 新文件名
            String fileName = OrderNumUtils.createOrderNum() + suffixName;

            Environment environment = ApplicationContextProvider.getApplicationContext().getBean(Environment.class);
            String profile = environment.getProperty("spring.profiles.active");
            String savePath;
            String readPath;
            switch (profile) {
                case "test":
                    savePath = environment.getProperty("file.savePath.test");
                    readPath = environment.getProperty("file.readPath.test");
                    break;
                case "pro":
                    savePath = environment.getProperty("file.savePath.pro");
                    readPath = environment.getProperty("file.readPath.pro");
                    break;
                default:
                    savePath = environment.getProperty("file.savePath.dev");
                    readPath = environment.getProperty("file.readPath.dev");
                    break;
            }
            String savePathName = savePath + fileName;
            String readPathName = readPath + fileName;

            File dest = new File(savePathName);
            FileUtils.judeDirExists(dest);
            Long l = file.getSize();
            if(l > 1024 * 1024 * 10){
                jsonObject.put("code", 200);
                jsonObject.put("msg", "请求成功");
                jsonObject.put("biz_code", "500");
                jsonObject.put("biz_msg", "上传文件不能大于10M");
            } else {
                file.transferTo(dest);
                jsonObject.put("code", 200);
                jsonObject.put("msg", "请求成功");
                jsonObject.put("biz_code", "200");
                jsonObject.put("biz_msg", "上传成功");
                jsonObject.put("path_name", readPathName);
            }
            log.sysInfoLog("uploadServerFile", "服务器文件上传   end");
        } catch (Exception e) {
            jsonObject.put("code", 200);
            jsonObject.put("msg", "请求成功");
            jsonObject.put("biz_code", "500");
            jsonObject.put("biz_msg", "上传文件失败" + e.getMessage());
            log.sysErrLog("uploadServerFile", "服务器文件上传失败", e);
        }
        return jsonObject;
    }

}
